package com.crm.CLdriving.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.crm.CLdriving.util.StringUtils;

import lombok.extern.log4j.Log4j2;

/**
 * 
 * 自定义拦截器
 * 
 * @author Administrator
 *
 */
@Log4j2
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	// 这个方法是在访问接口之前
	// 执行的，我们只需要在这里写验证登陆状态的业务逻辑，就可以在用户调用指定接口之前验证登陆状态了
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		// 从请求头获取token
		String token = request.getHeader("token");
		// 如果请求头没有token,则从请求参数中取
		if (!StringUtils.isValid(token)) {
			token = request.getParameter("token");
		}
		if (StringUtils.isValid(token)) {
			return true;
		}
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
			ModelAndView modelAndView) throws Exception {
		// log.info("----处理请求完成后视图渲染之前的处理操作---");
	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object o, Exception e) throws Exception {
		// log.info("-------视图渲染之后的操作---------");
	}

}
